Imports System.ComponentModel
Imports System.Xml.Serialization
Imports Aricie.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.DNN.UI.WebControls
Imports DotNetNuke.UI.WebControls
Imports Aricie.DNN.UI.WebControls.EditControls
Imports DotNetNuke.UI.Skins.Controls

Namespace Aricie.DNN.Caching.Analysis
    <Serializable()> _
    Public Class AutoConfigSettings


        Private _AnalysisFileName As String = "Aricie.DistributedCaching.Analysis.xml"
        Private _EnablePeriodicAnalysis As Boolean
        Private _TriggerKey As String = "GetHostSettings"
        Private _AnalysisPeriod As New STimeSpan(TimeSpan.FromHours(2))
        Private _ChangeDefaultSerializer As New STimeSpan(TimeSpan.FromMinutes(30))
        Private _SwitchIConvertible As New STimeSpan(TimeSpan.FromMinutes(20))
        Private _GenerateStatisticsPeriod As New STimeSpan(TimeSpan.FromMinutes(10))
        Private _ClearRegionsPeriod As New STimeSpan(TimeSpan.FromSeconds(90))
        Private _ClearLocalCachePeriod As New STimeSpan(TimeSpan.FromSeconds(30))

        'Private _OverrideStatistics As Boolean
        Private _SkipBinarySerializer As Boolean
        Private _AlwaysUpdateStatistics As Boolean

        Private _OverrideKeysOnAutoConfig As Boolean

        Private _NoMainGroups As Boolean
        Private _NoClusters As Boolean
        Private _MinGroupSize As Integer = 5
        Private _IndividualStrategies As Boolean



        Private _MaxCoefOfVariation As Integer = 70
        Private _MinLossForNoDistribution As Integer = 1000
        Private _MinGainForDistribution As Integer = 90
        Private _MaxLossForBatch As Integer = 600
        Private _MinPercentageNextKeyForGroup As Integer = 70
        Private _MinPercentagePrevKeyForGroup As Integer = 20


        

        <IsReadOnly(True)> _
        Public Property EnablePeriodicAnalysis() As Boolean
            Get
                Return _EnablePeriodicAnalysis
            End Get
            Set(ByVal value As Boolean)
                _EnablePeriodicAnalysis = value
            End Set
        End Property


        <ConditionalVisible("EnablePeriodicAnalysis", True)> _
        <ActionButton(IconName.ToggleOn, IconOptions.Normal, "AutoAnalysisOn.Warning")> _
        Public Overridable Sub EnableAutoAnalysis(pe As AriciePropertyEditorControl)
            'Dim settings As DistributedCachingProviderSettings = DistributedCachingProviderBase.Settings
            Dim settings As DistributedCachingProviderSettings = pe.GetParentEntity(Of DistributedCachingProviderSettings)()
            settings.Enabled = True
            settings.LocalCacheSettings.LocalEnabled = True
            settings.LocalCacheSettings.EnableGlobalDependency = True
            settings.CloudSettings.EnableSynchronization = False
            settings.CloudSettings.EnableDistributedCache = True
            settings.CloudSettings.EnforceXmlSerialization = True
            settings.CloudSettings.DefaultKeyConfig.DistributionType = CacheDistributionType.Undefined
            settings.CloudSettings.DefaultKeyConfig.RegionName = "AutoConfig"
            settings.CloudSettings.DefaultKeyConfig.EvictionsOn = False
            settings.CloudSettings.DefaultKeyConfig.DisableClusterRemoval = True
            settings.CloudSettings.DefaultKeyConfig.LogOperations = True
            settings.LoggingSettings.EnableDebugLogging = True
            settings.LoggingSettings.EnableStatsCollector = True
            settings.StatisticsSettings.UpdateExistingStats = True
            settings.AutoConfigSettings.EnablePeriodicAnalysis = True
            settings.Save()
            DistributedCachingBusinessController.AnalyzeTaskQueue.EnqueueTask(Now)
            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("AutoConfigOn.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub

        <ConditionalVisible("EnablePeriodicAnalysis")> _
        <ActionButton(IconName.ToggleOff, IconOptions.Normal, "AutoAnalysisOff.Warning")> _
        Public Overridable Sub DisableAutoAnalysis(pe As AriciePropertyEditorControl)

            Dim settings As DistributedCachingProviderSettings = pe.GetParentEntity(Of DistributedCachingProviderSettings)()
            settings.CloudSettings.EnableSynchronization = True
            settings.CloudSettings.EnforceXmlSerialization = False
            settings.CloudSettings.DefaultKeyConfig.RegionName = ""
            settings.CloudSettings.DefaultKeyConfig.EvictionsOn = True
            settings.CloudSettings.DefaultKeyConfig.DisableClusterRemoval = False
            settings.CloudSettings.DefaultKeyConfig.DistributionType = CacheDistributionType.NotDistributed
            settings.LoggingSettings.EnableStatsCollector = False
            settings.AutoConfigSettings.EnablePeriodicAnalysis = False
            'DistributedCachingBusinessController.SaveSettings(settings)
            settings.Save()

            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("AutoConfigOff.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub


        <ExtendedCategory("Results")> _
        Public Property ShowResults() As Boolean

        Private _Analysis As CacheAnalysis

        <ConditionalVisible("ShowResults")> _
        <ExtendedCategory("Results")> _
        <XmlIgnore()> _
        Public Property Analysis As CacheAnalysis
            Get
                If _Analysis Is Nothing AndAlso ShowResults Then
                    _Analysis = DistributedCachingBusinessController.LoadAnalysis()
                End If
                Return _Analysis
            End Get
            Set(value As CacheAnalysis)

            End Set
        End Property

        <ActionButton(IconName.Magic, IconOptions.Normal, "ComputeAnalysis.Warning")> _
        Public Overridable Sub ComputeAnalysis(pe As AriciePropertyEditorControl)

            DistributedCachingBusinessController.PerformAnalysisFromStatistics(DistributedCachingBusinessController.LoadStatistics(), DistributedCachingProviderBase.Settings)
            ShowResults = True
            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("AnalysisComputed.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub

        <ConditionalVisible("ShowResults")> _
        <ExtendedCategory("Results")> _
        <ActionButton(IconName.Magic, IconOptions.Normal, "ComputeAnalysis.Warning")> _
        Public Overridable Sub ApplyAnalysis(pe As AriciePropertyEditorControl)

            DistributedCachingBusinessController.ApplyAnalysisRecommandations(Analysis, DistributedCachingProviderBase.Settings)

            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("AnalysisApplied.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub

        <ConditionalVisible("ShowResults")> _
       <ExtendedCategory("Results")> _
       <ActionButton(IconName.FloppyO, IconOptions.Normal, "ComputeAnalysis.Warning")> _
        Public Overridable Sub SaveAnalysis(pe As AriciePropertyEditorControl)

            DistributedCachingBusinessController.SaveAnalysis(Analysis)
            _Analysis = Nothing

            pe.ItemChanged = True
            pe.DisplayLocalizedMessage("AnalysisSaved.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
        End Sub

        <ConditionalVisible("ShowResults")> _
       <ExtendedCategory("Results")> _
       <ActionButton(IconName.TrashO, IconOptions.Normal, "ComputeAnalysis.Warning")> _
        Public Overridable Sub ClearAnalysis(pe As AriciePropertyEditorControl)
            _Analysis = New CacheAnalysis
            pe.DisplayLocalizedMessage("AnalysisCleared.Message", ModuleMessage.ModuleMessageType.GreenSuccess)
            Me.SaveAnalysis(pe)
        End Sub

        <ExtendedCategory("Settings")> _
        <Required(True)> _
        Public Property AnalysisFileName() As String
            Get
                Return _AnalysisFileName
            End Get
            Set(ByVal value As String)
                _AnalysisFileName = value
            End Set
        End Property

        <ExtendedCategory("Settings")> _
        Public Property AlwaysUpdateStatistics() As Boolean
            Get
                Return _AlwaysUpdateStatistics
            End Get
            Set(ByVal value As Boolean)
                _AlwaysUpdateStatistics = value
            End Set
        End Property

        <ExtendedCategory("Settings")> _
        Public Property OverrideKeysOnAutoConfig() As Boolean
            Get
                Return _OverrideKeysOnAutoConfig
            End Get
            Set(ByVal value As Boolean)
                _OverrideKeysOnAutoConfig = value
            End Set
        End Property
        '<Category("UpdateSettings")> _
        'Public Property OverrideStatistics() As Boolean
        '    Get
        '        Return _OverrideStatistics
        '    End Get
        '    Set(ByVal value As Boolean)
        '        _OverrideStatistics = value
        '    End Set
        'End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
        Public Property TriggerKey() As String
            Get
                Return _TriggerKey
            End Get
            Set(ByVal value As String)
                _TriggerKey = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
        Public Property SkipBinarySerializer() As Boolean
            Get
                Return _SkipBinarySerializer
            End Get
            Set(ByVal value As Boolean)
                _SkipBinarySerializer = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
        Public Property AnalysisPeriod() As STimeSpan
            Get
                Return _AnalysisPeriod
            End Get
            Set(ByVal value As STimeSpan)
                _AnalysisPeriod = value
            End Set
        End Property

       

        <ExtendedCategory("Settings", "AutomationSettings")> _
       Public Property ChangeDefaultSerializer() As STimeSpan
            Get
                Return _ChangeDefaultSerializer
            End Get
            Set(ByVal value As STimeSpan)
                _ChangeDefaultSerializer = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
        Public Property SwitchIConvertiblePeriod() As STimeSpan
            Get
                Return _SwitchIConvertible
            End Get
            Set(ByVal value As STimeSpan)
                _SwitchIConvertible = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
        Public Property GenerateStatisticsPeriod() As STimeSpan
            Get
                Return _GenerateStatisticsPeriod
            End Get
            Set(ByVal value As STimeSpan)
                _GenerateStatisticsPeriod = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
       Public Property ClearRegionsPeriod() As STimeSpan
            Get
                Return _ClearRegionsPeriod
            End Get
            Set(ByVal value As STimeSpan)
                _ClearRegionsPeriod = value
            End Set
        End Property

        <ExtendedCategory("Settings", "AutomationSettings")> _
       Public Property ClearLocalCachePeriod() As STimeSpan
            Get
                Return _ClearLocalCachePeriod
            End Get
            Set(ByVal value As STimeSpan)
                _ClearLocalCachePeriod = value
            End Set
        End Property






        <ExtendedCategory("Settings", "GroupSettings")> _
        Public Property NoMainGroups() As Boolean
            Get
                Return _NoMainGroups
            End Get
            Set(ByVal value As Boolean)
                _NoMainGroups = value
            End Set
        End Property

        <ExtendedCategory("Settings", "GroupSettings")> _
        Public Property NoClusters() As Boolean
            Get
                Return _NoClusters
            End Get
            Set(ByVal value As Boolean)
                _NoClusters = value
            End Set
        End Property

        <ExtendedCategory("Settings", "GroupSettings")> _
            <Required(True)> _
       Public Property MinGroupSize() As Integer
            Get
                Return _MinGroupSize
            End Get
            Set(ByVal value As Integer)
                _MinGroupSize = value
            End Set
        End Property

        <ExtendedCategory("Settings", "GroupSettings")> _
       Public Property IndividualStrategies() As Boolean
            Get
                Return _IndividualStrategies
            End Get
            Set(ByVal value As Boolean)
                _IndividualStrategies = value
            End Set
        End Property



        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
        Public Property MaxCoefOfVariation() As Integer
            Get
                Return _MaxCoefOfVariation
            End Get
            Set(ByVal value As Integer)
                _MaxCoefOfVariation = value
            End Set
        End Property

        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
         Public Property MinLossForNoDistribution() As Integer
            Get
                Return _MinLossForNoDistribution
            End Get
            Set(ByVal value As Integer)
                _MinLossForNoDistribution = value
            End Set
        End Property


        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
         Public Property MinGainForDistribution() As Integer
            Get
                Return _MinGainForDistribution
            End Get
            Set(ByVal value As Integer)
                _MinGainForDistribution = value
            End Set
        End Property

        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
         Public Property MaxLossForBatch() As Integer
            Get
                Return _MaxLossForBatch
            End Get
            Set(ByVal value As Integer)
                _MaxLossForBatch = value
            End Set
        End Property

        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
         Public Property MinPercentageNextKeyForGroup() As Integer
            Get
                Return _MinPercentageNextKeyForGroup
            End Get
            Set(ByVal value As Integer)
                _MinPercentageNextKeyForGroup = value
            End Set
        End Property

        <ExtendedCategory("Settings", "ToleranceSettings")> _
            <Required(True)> _
         Public Property MinPercentagePrevKeyForGroup() As Integer
            Get
                Return _MinPercentagePrevKeyForGroup
            End Get
            Set(ByVal value As Integer)
                _MinPercentagePrevKeyForGroup = value
            End Set
        End Property




    End Class
End Namespace